package com.shuoyue.ycgk.ui.news.contract;

import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.News;
import com.shuoyue.ycgk.entity.NewsType;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import java.util.List;

import io.reactivex.Observable;

public interface NewsContract {
    interface View extends BaseView {
        void setNewsInfo(News newsInfo);

        void setNewsList(ListWithPage<News> newsList);

        void setNewsTypeList(List<NewsType> newsTypeList);
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        public void getNewsList(Integer moduleId,
                                Integer n,
                                Integer type,
                                String keywords) {
            apply(model.getNewsList(moduleId, n, type, keywords))
                    .subscribe(new ApiSubscriber<Optional<ListWithPage<News>>>(mView, this) {
                        @Override
                        public void onNext(Optional<ListWithPage<News>> listWithPageOptional) {
                            super.onNext(listWithPageOptional);
                            mView.setNewsList(listWithPageOptional.getIncludeNull());
                        }
                    });
        }

        public void getNewsInfo(int id) {
            apply(model.getNewsInfo(id))
                    .subscribe(new ApiSubscriber<Optional<News>>(mView, this) {
                        @Override
                        public void onNext(Optional<News> optional) {
                            super.onNext(optional);
                            mView.setNewsInfo(optional.getIncludeNull());
                        }
                    });
        }
        public void getNewsType(int id) {
            apply(model.newsType(id))
                    .subscribe(new ApiSubscriber<Optional<List<NewsType>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<NewsType>> optional) {
                            super.onNext(optional);
                            mView.setNewsTypeList(optional.getIncludeNull());
                        }
                    });
        }
    }

    class Model {
        Observable<BaseResult<ListWithPage<News>>> getNewsList(Integer moduleId,
                                                               Integer n,
                                                               Integer type,
                                                               String keywords) {
            return RetrofitClient.getInstance().getApi().informationList(moduleId, n, type, keywords);
        }

        Observable<BaseResult<News>> getNewsInfo(int id) {
            return RetrofitClient.getInstance().getApi().information(id);
        }

        Observable<BaseResult<List<NewsType>>> newsType(int id) {
            return RetrofitClient.getInstance().getApi().infomationType(id);
        }

    }
}
